<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body custom-class="import-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="人脸照片" prop="img">
          <ImageUpload v-model="form.img" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="loading">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { debounce } from '@/utils'
import { reAuthFace } from '@/api/smartLock/lockManage'

export default {
  components: {
  },
  props: {
    title: {
      default: '授权租客人脸'
    },
    value: {
      default: false
    },
    data: {
      default: undefined
    }
  },
  data() {
    return {
      //是否显示选择房间弹窗
      dialogSelectRoomVisible: false,
      form: {},
      rules: {
        userName: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '请上传人脸照片', trigger: 'blur' }
        ]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: []
    }
  },
  computed: {
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.init()
        }
      },
      immediate: true
    }
  },
  methods: {
    init() {
      this.reset()
    },
    reset() {
      this.form = {
        faceId: this.data.faceId,
        userName: this.data.userName,
        img: ''
      }
      this.resetForm('form')
    },
    // 确认
    submitForm: debounce(function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          reAuthFace(this.form).then(response => {
            this.loading = false
            if (response.code == 200) {
              this.$emit('success')
              this.$message.success(response.msg)
              this.visible = false
            } else {
              this.$message.error(response.msg)
            }
          }).catch(()=>{
            this.loading = false
          })
        }
      })
    }, 500, false),
    cancel() {
      this.reset()
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog";
</style>
